package First;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.sound.sampled.*;

class ImageScrollPane extends JScrollPane {
    private Image backgroundImage;

    public ImageScrollPane(Component view, Image backgroundImage) {
        super(view);
        this.backgroundImage = backgroundImage;
        setOpaque(false);
        getViewport().setOpaque(false);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
    }
}

public class MainPage extends JFrame {
    private JTable table;
    private String filePath;

    public MainPage() {
        filePath = System.getProperty("user.home") + "/Desktop/students.txt";
        createView();

        setTitle("学生管理系统 - 主页面");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null);
        setResizable(false);

        try {
            File audioFile = new File("C:\\Users\\Exist\\IdeaProjects\\untitled\\src\\background.wav");
            AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
            AudioFormat format = audioStream.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class, format);
            Clip audioClip = (Clip) AudioSystem.getLine(info);
            audioClip.open(audioStream);
            audioClip.start();
            audioClip.loop(Clip.LOOP_CONTINUOUSLY);
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
            e.printStackTrace();
        }
    }

    private void createView() {
        Image backgroundImage = Toolkit.getDefaultToolkit().createImage("C:\\Users\\Exist\\IdeaProjects\\untitled\\src\\background.jpg");

        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
            }
        };
        panel.setLayout(new BorderLayout());

        String[] columnNames = {"学号", "密码", "姓名", "班级", "年龄", "专业"};
        DefaultTableModel model = new DefaultTableModel(columnNames, 0);
        table = new JTable(model);

        ImageScrollPane scrollPane = new ImageScrollPane(table, backgroundImage);
        panel.add(scrollPane, BorderLayout.CENTER);

        getContentPane().add(panel);

        JPanel northPanel = new JPanel();
        northPanel.setOpaque(false);
        northPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        panel.add(northPanel, BorderLayout.NORTH);

        JButton addButton = new JButton("添加学生");
        northPanel.add(addButton);

        JButton deleteButton = new JButton("删除学生");
        northPanel.add(deleteButton);

        JButton editButton = new JButton("编辑学生");
        northPanel.add(editButton);

        JButton refreshButton = new JButton("刷新列表");
        northPanel.add(refreshButton);

        // 添加搜索功能
        JTextField searchField = new JTextField(20);
        searchField.setForeground(Color.gray);
        searchField.setText("输入学生基本信息以搜索~");
        JButton searchButton = new JButton("搜索");

        northPanel.add(new JLabel("关键字:"));
        northPanel.add(searchField);
        northPanel.add(searchButton);

        searchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String keyword = searchField.getText().trim().toLowerCase();

                DefaultTableModel model = (DefaultTableModel) table.getModel();
                TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<>(model);
                table.setRowSorter(sorter);

                RowFilter<DefaultTableModel, Object> rowFilter = RowFilter.regexFilter(keyword);
                sorter.setRowFilter(rowFilter);
            }
        });

    }

    private void saveToFile() {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        int rowCount = model.getRowCount();

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            for (int row = 0; row < rowCount; row++) {
                for (int col = 0; col < model.getColumnCount(); col++) {
                    writer.write(model.getValueAt(row, col).toString());
                    if (col < model.getColumnCount() - 1) {
                        writer.write(",");
                    }
                }
                writer.newLine();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
